package com.ssy.lingxi.search.controller.feign;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.channel.ChannelCommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.channel.TemplateChannelCommoditySearchRequest;
import com.ssy.lingxi.search.api.model.response.BrandResponse;
import com.ssy.lingxi.search.api.model.response.CategoryResponse;
import com.ssy.lingxi.search.api.model.response.channel.ChannelCommoditySearchResponse;
import com.ssy.lingxi.search.entity.Commodity;
import com.ssy.lingxi.search.entity.CommoditySoldCount;
import com.ssy.lingxi.search.entity.channel.ChannelCommodityMember;
import com.ssy.lingxi.search.service.IChannelShopService;
import com.ssy.lingxi.search.service.ITemplateChannelService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模板服务类--渠道商品
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/21
 */
@RestController
@RequestMapping("/search/channelCommodity/template")
public class TemplateChannelController extends BaseController {

    @Resource
    private IChannelShopService channelShopService;

    @Resource
    private ITemplateChannelService templateChannelCommodityService;

    /**
     * 查询一级品类信息
     * @return
     */
    @RequestMapping(value = "/getFirstCategoryListByMemberId", method = RequestMethod.GET)
    public Wrapper<List<CategoryResponse>> getFirstCategoryList(
            @RequestParam(value = "shopId") Long shopId,
            @RequestParam(value = "memberId") Long memberId){
        return Wrapper.success(templateChannelCommodityService.getFirstCategoryList(shopId, memberId));
    }

    /**
     * 查询二级品类信息
     * @return
     */
    @RequestMapping(value = "/getSecondCategoryListByMemberId", method = RequestMethod.GET)
    public Wrapper<List<CategoryResponse>> getSecondCategoryList(
            @RequestParam(value = "shopId") Long shopId,
            @RequestParam(value = "memberId") Long memberId,
            @RequestParam(value = "categoryId") Long categoryId){
        return Wrapper.success(templateChannelCommodityService.getSecondCategoryList(shopId, memberId, categoryId));
    }

    /**
     * 查询品牌信息
     * @param current 当前页码
     * @param pageSize 每页条数
     * @param customerCategoryId 会员分类id
     * @param categoryId 平台分类id
     * @param memberId 会员id
     * @param name 品牌名称
     * @param idInList 包含
     * @param idNotInList 不包含
     * @return
     */
    @RequestMapping(value = "/getBrandList", method = RequestMethod.GET)
    public Wrapper<PageData<BrandResponse>> getBrandList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize,
                                                         @RequestParam(value = "shopId", required = false) Long shopId,
                                                         @RequestParam(value = "customerCategoryId",required = false) Long customerCategoryId,
                                                         @RequestParam(value = "categoryId",required = false) Long categoryId,
                                                         @RequestParam(value = "memberId",required = false) Long memberId,
                                                         @RequestParam(value = "memberRoleId",required = false) Long memberRoleId,
                                                         @RequestParam(value = "memberName",required = false) String memberName,
                                                         @RequestParam(value = "name",required = false) String name,
                                                         @RequestParam(value = "idInList",required = false) List<Long> idInList,
                                                         @RequestParam(value = "idNotInList",required = false) List<Long> idNotInList){
        //构造查询条件
        PageVO pageVO = new PageVO();
        pageVO.setCurrent(current);
        pageVO.setPageSize(pageSize);
        CommoditySearchRequest commoditySearchRequest = new CommoditySearchRequest();
        commoditySearchRequest.setCategoryId(categoryId);
        commoditySearchRequest.setCustomerCategoryId(customerCategoryId);
        commoditySearchRequest.setName(name);
        commoditySearchRequest.setMemberId(memberId);
        commoditySearchRequest.setMemberRoleId(memberRoleId);
        commoditySearchRequest.setMemberName(memberName);
        commoditySearchRequest.setIdInList(idInList);
        commoditySearchRequest.setIdNotInList(idNotInList);
        commoditySearchRequest.setShopId(shopId);

        return Wrapper.success(templateChannelCommodityService.searchBrandList(pageVO, commoditySearchRequest));
    }

    /**
     * 查询商品信息
     * @param templateChannelCommoditySearchRequest 参数
     */
    @RequestMapping(value = "/searchCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<ChannelCommoditySearchResponse>> searchCommodityList(@Valid @RequestBody TemplateChannelCommoditySearchRequest templateChannelCommoditySearchRequest)  {
        Long shopId = templateChannelCommoditySearchRequest.getShopId();
        Long memberId = templateChannelCommoditySearchRequest.getMemberId();
        Long memberRoleId = templateChannelCommoditySearchRequest.getMemberRoleId();
        Long loginMemberId = templateChannelCommoditySearchRequest.getLoginMemberId();
        Long loginMemberRoleId = templateChannelCommoditySearchRequest.getLoginMemberRoleId();
        //构造查询条件
        ChannelCommoditySearchRequest channelCommoditySearchRequest = BeanUtil.copyProperties(templateChannelCommoditySearchRequest, ChannelCommoditySearchRequest.class);
        channelCommoditySearchRequest.setChildChannelMemberId(loginMemberId);
        channelCommoditySearchRequest.setChildChannelMemberRoleId(loginMemberRoleId);
        channelCommoditySearchRequest.setChannelMemberId(memberId);
        channelCommoditySearchRequest.setChannelMemberRoleId(memberRoleId);

        PageData<Commodity> result = channelShopService.getCommodityList(channelCommoditySearchRequest, false);
        String memberName = "";
        List<ChannelCommoditySearchResponse> resultList = result.getData().stream().map(commodity -> {
            ChannelCommoditySearchResponse channelCommoditySearchResponse = this.modelMapper.map(commodity, ChannelCommoditySearchResponse.class);
            Double max = 0D;
            Double min = 0D;
            double sold = 0;

            List<ChannelCommodityMember> memberList = commodity.getMemberList();
            if(memberList != null && memberList.size() > 0){
                for (ChannelCommodityMember channelCommodityMember : memberList) {
                    Double memberMax = channelCommodityMember.getMax();
                    Double memberMin = channelCommodityMember.getMin();
                    if (memberMax > max) {
                        max = memberMax;
                    }
                    if (min == 0) {
                        min = memberMin;
                    }
                    if (memberMin < min) {
                        min = memberMin;
                    }
                    //不同商城显示不同的已售数量
                    List<CommoditySoldCount> commoditySoldCountList = channelCommodityMember.getCommoditySoldCountList();
                    if (commoditySoldCountList != null && commoditySoldCountList.size() > 0) {
                        for(CommoditySoldCount commoditySoldCount : commoditySoldCountList){
                            Long commoditySoldCountShopId = commoditySoldCount.getShopId();
                            if (commoditySoldCountShopId.longValue() == shopId.longValue()) {
                                sold = commoditySoldCount.getSold();
                            }
                        }
                    }
                }
            }

            channelCommoditySearchResponse.setMax(max);
            channelCommoditySearchResponse.setMin(min);
            channelCommoditySearchResponse.setMemberId(memberId);
            channelCommoditySearchResponse.setMemberName(memberName);
            channelCommoditySearchResponse.setMemberRoleId(memberRoleId);
            channelCommoditySearchResponse.setSold(sold);
            return channelCommoditySearchResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalCount(), resultList));
    }
}
